package com.stone.mythread.juc;

public class ThreadMain {

    private int ticket = 30;

    public synchronized void sale() {
        while (ticket > 0) {
            System.err.println(Thread.currentThread().getName() + " 卖出 " + (ticket--) + " 剩下 " + ticket);
        }
    }

    public static void main(String[] args) {
        ThreadMain ticket = new ThreadMain();

        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        }, "AA").start();

        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        }, "BB").start();

        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        }, "CC").start();
    }
}
